var CommonUtil = {
    setCookie: function (name, value) {
        var argv = CommonUtil.setCookie.arguments;
        var argc = CommonUtil.setCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        if (expires != null) {
            var LargeExpDate = new Date();
            LargeExpDate.setTime(LargeExpDate.getTime() + (expires * 1000 * 3600 * 24));
        }
        document.cookie = name + "=" + escape(value) + ((expires == null) ? "" :
            ("; expires=" + LargeExpDate.toGMTString()));
    },
    getCookie: function (name) {
        var search = name + "=";
        if (document.cookie.length > 0) {
            offset = document.cookie.indexOf(search);
            if (offset != -1) {
                offset += search.length;
                end = document.cookie.indexOf(";", offset);
                if (end == -1) end = document.cookie.length;
                return unescape(document.cookie.substring(offset, end))
            }
            else return ""
        }
    },
    deleteCookie: function (name) {
        var expdate = new Date();
        expdate.setTime(expdate.getTime() - (86400 * 1000));
        setCookie(name, "", expdate);
    },
    //格式化时间
    timeFormat: function (nS) {
        return new Date(parseInt(("/Date(" + nS + ")/").substr(6, 13))).toLocaleDateString();
    },
    //格式化标签
    labelFormat: function (label) {
        var labelArr = label.split(';');
        var formatLabelArr = [];
        for (var i = 0; i < labelArr.length; i++) {
            switch (labelArr[i]) {
                case '0':
                    formatLabelArr[i] = '#美食#';
                    break;
                case '1':
                    formatLabelArr[i] = '#景点#';
                    break;
                case '2':
                    formatLabelArr[i] = '#出行#';
                    break;
                case '3':
                    formatLabelArr[i] = '#住宿#';
                    break;
            }
        }

        return formatLabelArr;
    },
    //格式化imagepath
    formatImagePath : function (imagePathList) {
        return imagePathList.split(';');
    },
    isScrollToPageBottom: function () {
        //文档高度
        var documentHeight = document.documentElement.offsetHeight;
        var viewPortHeight = CommonUtil.getViewportSize().h;
        var scrollHeight = window.pageYOffset ||
            document.documentElement.scrollTop ||
            document.body.scrollTop || 0;

        return documentHeight - viewPortHeight - scrollHeight < 20;
    },
    getViewportSize: function (w) {
        //使用指定的窗口， 如果不带参数则使用当前窗口
        w = w || window;

        //除了IE8及更早的版本以外，其他浏览器都能用
        if (w.innerWidth != null)
            return {w: w.innerWidth, h: w.innerHeight};

        //对标准模式下的IE（或任意浏览器）
        var d = w.document;
        if (document.compatMode == "CSS1Compat")
            return {w: d.documentElement.clientWidth, h: d.documentElement.clientHeight};

        //对怪异模式下的浏览器
        return {w: d.body.clientWidth, h: d.body.clientHeight};
    },
    //判断是否登录并且 登录返回用户信息 不登录返回null
    isLogin: function () {
        var userInfoJson = CommonUtil.getCookie('userInfo');
        if (userInfoJson != null) {
            if (userInfoJson.length > 0) {
                return JSON.parse(userInfoJson);
            }
        }
        return null;
    }
};
